<?php
namespace App\Controllers\Passport;
use App\Models\User\User;
use App\Libs\Helper\WeixinHelper;
use App\Libs\Base\Constant;
/**
*	登录
*/
class Login extends \App\Libs\Base\BaseAction
{
    protected function beforeRun() {
        parent::beforeRun();
    }

	protected function execute() {
		//微信登录
		$wx_code = $this->request->get('code');
		if( !empty( $wx_code ) ){
			return self::wxLogin( $wx_code );
		}
		
		//微信第一次绑定
		$wx_unionid = $this->request->get( 'unionid' );
		if( !empty( $wx_unionid ) ){
			$wx_frist['phone'] = $this->request->get('phone');//手机号
			$captcha = $this->request->get('captcha');//验证码
			$wx_frist['openid'] = $this->request->get('openid');//openid
			$wx_frist['headimgurl'] = $this->request->get('headimgurl');//头像
			$wx_frist['username'] = $this->request->get('nickname');//用户名
			return self::wxFristBind( $wx_unionid , $wx_frist , $captcha  );
		}
		
		//手机号登录
		$phone = $this->request->get('phone');//手机号
		$captcha = $this->request->get('captcha');//验证码
		return self::phoneLogin( $phone , $captcha );
	}
	
	/**
	*	微信第一次登录绑定
	*/
	public static function wxFristBind( $wx_unionid , $wx_frist , $captcha ){
		//验证手机号是否已绑定
		$user = User::find(array(
			'phone = ?0',
			'bind' => array($wx_frist['phone']),
		));
		$wx_frist['unionid'] = $wx_unionid;
		if( empty( $user ) ){
			//不存在账号，验证验证码 然后注册
			$user = new User();
			$user->phone = $wx_frist['phone'];
			if ($user->loginCaptcha($captcha)) {
				$id = $user->regist( $wx_frist );
				$user->type = 0;//未注册值
			}else{
				return \App\Libs\Views\Json::view(Constant::INVALID_CAPTCHA, '验证码无效', $phone);
			}
		}else{
			//存在账号
			//验证手机号是否已绑定
			if( !empty( $user->unionid ) ){
				return \App\Libs\Views\Json::view( 1 , '该手机号已绑定微信', NULL );
			}
			//未绑定 验证验证码 ，然后修改信息
			if ($user->loginCaptcha($captcha)) {
				$user->openid = $wx_frist['openid'];
				$user->unionid = $wx_frist['unionid'];
				$user->save();
				$user->type = 1;//已注册值
			}else{
				return \App\Libs\Views\Json::view(Constant::INVALID_CAPTCHA, '验证码无效', null);
			}
		}
		
		
		//验证验证码是否正确
		//用户存在  返回的用户信息
		$data = array(
			'token' => $user->token,
			'name' => $user->username,
			'avatar' => $user->avatar,
			'baby_name' => $user->baby_name,
			'baby_sex' => $user->baby_sex,
			'baby_birthday' => $user->baby_birthday,
			'age' => \App\Libs\Helper\CommonHelper::age($user->baby_birthday),
			'vip' => $user->vip == 1?'已开通':'未开通',
			'balance' => $user->balance,
			'type' => $user->type,
			'phone' => $user->phone,
		 );
		 return \App\Libs\Views\Json::view(0, 'ok', $data);	
		
	}
	
	
	/**
	*	手机号登录
	*/
	private static function phoneLogin( $phone , $captcha ){
		//手机号登录
		
		$user = User::find(array(
			'phone = ?0',
			'bind' => array($phone),
		));
		
		//用户不存在，直接注册
		if (empty($user)) {
			// 注册
			$user = new User();
			$user->phone = $phone;
			if ($user->loginCaptcha($captcha)) {
				$id = $user->regist( [ 'phone' => $phone ] );
				$user->type = 0;//未注册值
			}else{
				return \App\Libs\Views\Json::view(Constant::INVALID_CAPTCHA, '验证码无效', $phone);
			}
		}else{
			$user->type = 1;//已注册值
		}
		
		//用户存在  返回的用户信息
		if( !isset($user->vip) ){
			$user->vip = 0;
		}
		if( !isset($user->balance) ){
			$user->balance = 0;
		}
		$data = array(
			'token' => $user->token,
			'name' => $user->username,
			'avatar' => $user->avatar,
			'baby_name' => $user->baby_name,
			'baby_sex' => $user->baby_sex,
			'baby_birthday' => $user->baby_birthday,
			'age' => \App\Libs\Helper\CommonHelper::age($user->baby_birthday),
			'vip' => $user->vip == 1?'已开通':'未开通',
			'balance' => $user->balance,
			'type' => $user->type,
			'phone' => $user->phone,
		 );
		if (!empty($captcha)) {
			//验证码不为空
			if ($user->loginCaptcha($captcha)) {
				return \App\Libs\Views\Json::view(0, 'ok', $data);	
			}
			return \App\Libs\Views\Json::view(Constant::INVALID_CAPTCHA, '验证码无效', null);
		} else {
			//验证码为空
			/*$password = $this->request->get('password');
			if ($user->login($password)) {
				return \App\Libs\Views\Json::view(0, 'ok', $data);	
			}
			return \App\Libs\Views\Json::view(Constant::PASSWORD_ERROR, 'password failed', null);*/
			return \App\Libs\Views\Json::view( 1 , '验证码不能为空', $phone);
		}
	}
	
	/**
	*	微信登录
	*/
	private static function wxLogin( $wx_code ){
		$wx_user_info = WeixinHelper::getUserinfo( $wx_code );
		$wx_user = User::find(array(
			'unionid = ?0',
			'bind' => array($wx_user_info['unionid']),
		));
		if( empty( $wx_user ) ){
			//微信 第一次登录
			if( empty($wx_user_info) ){
				return \App\Libs\Views\Json::view(1, '获取微信信息失败', null);
			}
			$wx_user_info['typeCode'] = Constant::WX_FRIST_LOGIN;
			return \App\Libs\Views\Json::view( 0 , '获取微信信息成功', $wx_user_info);
		}else{
			//微信登录返回
			$user = $wx_user;
			//用户存在  返回的用户信息
			$data = array(
				'token' => $user->token,
				'name' => $user->username,
				'avatar' => $user->avatar,
				'baby_name' => $user->baby_name,
				'baby_sex' => $user->baby_sex,
				'baby_birthday' => $user->baby_birthday,
				'age' => \App\Libs\Helper\CommonHelper::age($user->baby_birthday),
				'vip' => $user->vip == 1?'已开通':'未开通',
				'balance' => $user->balance,
				'type' => 1,
				'phone' => $user->phone,
				'typeCode' => 1
			 );
			 return \App\Libs\Views\Json::view(0, 'ok', $data);	
		}
	}
	
}
